/*
 * Copyright (c) 2013-2014, starteasy Inc. All Rights Reserved.
 * 
 * Project Name: sample
 * $Id:  2016-11-14 17:11:35 $ 
 */
package cn.starteasy.sample.web.controller.api;

import cn.starteasy.core.common.domain.persistent.SearchEnum;
import cn.starteasy.core.common.domain.persistent.utils.ConditionBuilder;
import cn.starteasy.core.common.domain.wrapper.BooleanWrapper;
import cn.starteasy.sample.service.IProvinceService;
import cn.starteasy.core.common.domain.view.admin.SearchField;
import cn.starteasy.core.common.domain.wrapper.StringWrapper;
import cn.starteasy.core.common.domain.view.BizData4Page;
import cn.starteasy.core.common.exception.BizException;
import cn.starteasy.core.common.protocol.utils.RtnCodeEnum;

import cn.starteasy.sample.domain.Province;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/api")
public class ApiProvinceController{
    private static final Logger logger = LoggerFactory.getLogger(ApiProvinceController.class);

    @Autowired
    private IProvinceService provinceService;

   /**
     * 新增 省份
     * @param province
     * @return  处理条数
     */
    @ResponseBody
    @RequestMapping(value = "/province", method = RequestMethod.POST)
    public Object addProvince(@RequestBody Province province) {
        try {
            String msg = "";

            if(province==null) {
                msg = "添加省份参数不正确";
            }else if(province.getStatus() != null){
                msg = "省份状态不能为空";
            }else if(province.getCreator() != null){
                msg = "省份创建人不能为空";
            }else if(province.getCreateDate() != null){
                msg = "省份创建时间不能为空";
            }else if(province.getLastModifier() != null){
                msg = "省份修改人不能为空";
            }else if(province.getLastModDate() != null){
                msg = "省份修改时间不能为空";
            }

            if(StringUtils.isNotBlank(msg)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), msg);
            }

            provinceService.create(province);
            return new BooleanWrapper(true);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "新增省份异常");
        }
    }


    /**
     * 删除 省份
     *
     * @param {provinceId} 省份ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/province/{provinceId}" ,method = RequestMethod.DELETE)
    public Object delProvince(@PathVariable String provinceId) {
        try {
            if(StringUtils.isBlank(provinceId)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "删除省份失败，参数不正确");
            }
            provinceService.deleteByCondition(ConditionBuilder.condition("id", SearchEnum.eq, provinceId));
            return new BooleanWrapper(true);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "删除省份异常");
        }
    }


    /**
     * 修改省份数据
     * @param province 提交上来的省份JSON数据
     * @param provinceId  省份ID
     * @return  修改条数
     */
    @ResponseBody
    @RequestMapping(value = "/province/{provinceId}", method = RequestMethod.PATCH)
    public StringWrapper editProvince(@RequestBody Province province, @PathVariable String provinceId) {
        try {
            String msg = "";
            if(province==null) {
                msg = "添加省份参数不正确";
            }else if(province.getStatus() != null){
                msg = "省份状态不能为空";
            }else if(province.getCreator() != null){
                msg = "省份创建人不能为空";
            }else if(province.getCreateDate() != null){
                msg = "省份创建时间不能为空";
            }else if(province.getLastModifier() != null){
                msg = "省份修改人不能为空";
            }else if(province.getLastModDate() != null){
                msg = "省份修改时间不能为空";

            }

            if(StringUtils.isNotBlank(msg)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), msg);
            }

            Province province_old = (Province) provinceService.view(province.getId());
            if(province==null) {
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改失败，系统未找到相关数据");
            }

            if(province.getId() != null){
                province_old.setId(province.getId());
            }
            if(!StringUtils.isBlank(province.getName())){
                province_old.setName(province.getName());
            }
            if(!StringUtils.isBlank(province.getNumber())){
                province_old.setNumber(province.getNumber());
            }
            if(!StringUtils.isBlank(province.getXpoint())){
                province_old.setXpoint(province.getXpoint());
            }
            if(!StringUtils.isBlank(province.getYpoint())){
                province_old.setYpoint(province.getYpoint());
            }
            if(!StringUtils.isBlank(province.getCountryId())){
                province_old.setCountryId(province.getCountryId());
            }
            if(province.getStatus() != null){
                province_old.setStatus(province.getStatus());
            }
            if(province.getCreator() != null){
                province_old.setCreator(province.getCreator());
            }
            if(province.getCreateDate() != null){
                province_old.setCreateDate(province.getCreateDate());
            }
            if(province.getLastModifier() != null){
                province_old.setLastModifier(province.getLastModifier());
            }
            if(province.getLastModDate() != null){
                province_old.setLastModDate(province.getLastModDate());
            }


            int row=provinceService.edit(province_old);
            if(row > 0) {
                return new StringWrapper("修改省份成功");
            }else{
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改省份失败");
            }
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改省份数据异常");
        }
    }


    /**
     * 获取单省份实体
     *
     * @param provinceId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/province/{provinceId}", method = RequestMethod.GET)
    public Province getProvince(@PathVariable String provinceId) {
        try {
            if(StringUtils.isBlank(provinceId)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "参数不正确！");
            }
            Map<String,Object> whereParams = new HashMap<String, Object>();
            whereParams.put("id", provinceId);
            Province province= (Province) provinceService.viewOne(null, whereParams, null);
            if(null == province){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "系统未找到省份相关数据！");
            }
            return province;
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "获取单省份实体异常");
        }
    }


    /**
     * 省份数据输出 带分页
     *
     * @param province 过滤条件
     * @param page      第几页
     * @return 业务数据列表实体，最终转换为json数据输出
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/provincelist", method = RequestMethod.POST)
    public BizData4Page provincelist(Province province, Integer page) {
        try {
            Map<String, Object> whereParams = new HashMap<String, Object>();
            if(province.getId() != null){
                whereParams.put("id", new SearchField("id", "=", province.getId()));
            }
            if(!StringUtils.isBlank(province.getName())){
                whereParams.put("name", new SearchField("name", "like", "%" + province.getName() + "%"));
            }
            if(!StringUtils.isBlank(province.getNumber())){
                whereParams.put("number", new SearchField("number", "like", "%" + province.getNumber() + "%"));
            }
            if(!StringUtils.isBlank(province.getXpoint())){
                whereParams.put("xpoint", new SearchField("xpoint", "like", "%" + province.getXpoint() + "%"));
            }
            if(!StringUtils.isBlank(province.getYpoint())){
                whereParams.put("ypoint", new SearchField("ypoint", "like", "%" + province.getYpoint() + "%"));
            }
            if(!StringUtils.isBlank(province.getCountryId())){
                whereParams.put("countryId", new SearchField("countryId", "like", "%" + province.getCountryId() + "%"));
            }
            if(province.getStatus() != null){
                whereParams.put("status", new SearchField("status", "=", province.getStatus()));
            }
            if(province.getCreator() != null){
                whereParams.put("creator", new SearchField("creator", "=", province.getCreator()));
            }
            if(province.getCreateDate() != null){
                whereParams.put("createDate", new SearchField("createDate", "=", province.getCreateDate()));
            }
            if(province.getLastModifier() != null){
                whereParams.put("lastModifier", new SearchField("lastModifier", "=", province.getLastModifier()));
            }
            if(province.getLastModDate() != null){
                whereParams.put("lastModDate", new SearchField("lastModDate", "=", province.getLastModDate()));
            }


            //other props filter
            whereParams.put("groupOp", "and");

        return provinceService.queryPage(null, whereParams, page, 10, 10, null);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "查询省份数据异常");
        }
    }
}
